import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import _difference from "lodash/difference";
import _uniq from "lodash/uniq";
import _isEmpty from "lodash/isEmpty";
import _isArray from "lodash/isArray";
import _isUndefined from "lodash/isUndefined";
var _excluded = ["_status", "apiTags", "pageTag"],
    _excluded2 = ["__id", "_status"];
import intl from 'utils/intl';
import { CPAAS_IAM } from 'utils/config';
import { filterNullValueObject, getCurrentOrganizationId, isTenantRoleLevel } from 'utils/utils';
var organizationId = getCurrentOrganizationId();
var optionDs = new _DataSet({
  selection: 'single',
  autoQuery: true,
  paging: false,
  transport: {
    read: function read() {
      return {
        url: isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/labels/by-type") : "".concat(CPAAS_IAM, "/v1/labels/by-type"),
        method: 'GET',
        params: {
          type: 'API'
        }
      };
    }
  }
}); // API管理查询from

var formDS = {
  fields: [{
    name: 'code',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.code').d('权限编码')
  }, {
    name: 'path',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.path').d('路径')
  }, {
    name: 'serviceNameLov',
    type: 'object',
    lovCode: 'CADM.SERVICE_CODE',
    label: intl.get('ciam.apiManagement.model.apiManagement.serviceName').d('服务名称'),
    ignore: 'always',
    noCache: true
  }, {
    name: 'serviceName',
    type: 'string',
    bind: 'serviceNameLov.serviceName'
  }, {
    name: 'fdLevel',
    type: 'string',
    lookupCode: 'CIAM.PERMISSION_LEVEL',
    label: intl.get('ciam.apiManagement.model.apiManagement.fdLevel').d('权限层级')
  }, {
    name: 'method',
    type: 'string',
    lookupCode: 'CIAM.REQUEST_METHOD',
    lovPara: {
      tag: 'show'
    },
    label: intl.get('ciam.apiManagement.model.apiManagement.method').d('请求方式')
  }, {
    name: 'labels',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tag').d('标签'),
    valueField: 'name',
    textField: 'name',
    transformRequest: function transformRequest(value) {
      return value.join(',');
    },
    multiple: true,
    options: optionDs
  }, {
    name: 'publicAccess',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.publicAccess').d('是否公开接口')
  }, {
    name: 'loginAccess',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.loginAccess').d('是否登录可访问')
  }, {
    name: 'within',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.within').d('是否内部接口')
  }, {
    name: 'signAccess',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.signAccess').d('是否签名接口')
  }]
};

function tableDS(formRecord) {
  return {
    dataKey: 'content',
    cacheSelection: true,
    fields: [{
      name: 'id',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.id').d('权限id'),
      unique: true
    }, {
      name: 'code',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.code').d('权限编码')
    }, {
      name: 'path',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.path').d('路径')
    }, {
      name: 'method',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.method').d('请求方式')
    }, {
      name: 'methodMeaning',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.methodMeaning').d('请求方式')
    }, {
      name: 'fdLevel',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.fieldLevel').d('权限层级')
    }, {
      name: 'levelMeaning',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.levelMeaning').d('权限层级')
    }, {
      name: 'description',
      type: 'intl',
      label: intl.get('ciam.apiManagement.model.apiManagement.description').d('描述'),
      maxLength: 1024
    }, {
      name: 'action',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.action').d('方法名')
    }, {
      name: 'resource',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.resource').d('资源类型')
    }, {
      name: 'serviceName',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.serviceName').d('服务名称')
    }, {
      name: 'publicAccess',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.publicAccess').d('是否公开接口')
    }, {
      name: 'loginAccess',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.loginAccess').d('是否登录可访问')
    }, {
      name: 'within',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.within').d('是否内部接口')
    }, {
      name: 'signAccess',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.signAccess').d('是否签名接口')
    }, {
      name: 'objectVersionNumber',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.objectVersionNumber').d('版本')
    }, {
      name: 'tag',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.tag').d('标签')
    }, {
      name: 'pageTag',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.pageTag').d('API标签'),
      bind: 'apiTags.PAGE'
    } // {
    //   name: 'backgroundTag',
    //   type: 'string',
    //   label: intl.get('ciam.apiManagement.model.apiManagement.backgroundTag').d('后端API标识'),
    //   bind: 'apiTags.BACKEND',
    // },
    ],
    events: {
      query: function query(_ref) {
        var dataSet = _ref.dataSet;
        dataSet.unSelectAll();
        dataSet.clearCachedSelected();
      },
      update: function update(_ref2) {
        var record = _ref2.record,
            dataSet = _ref2.dataSet,
            value = _ref2.value,
            oldValue = _ref2.oldValue;

        // if (!isEmpty(dataSet.frontLabels)) {
        if (_isArray(value) || _isArray(oldValue)) {
          if ((value || []).sort().toString() !== (oldValue || []).sort().toString()) {
            if (_isEmpty(value)) {
              var PAGE = _uniq(oldValue.filter(function (item) {
                return (dataSet.frontLabels || []).includes(item);
              }));

              record.set('apiTags', {
                PAGE: PAGE,
                BACKEND: []
              });
            } else if ((dataSet.frontLabels || []).includes(_difference(oldValue, value)[0])) {
              record.set('apiTags', {
                PAGE: oldValue,
                BACKEND: []
              });
            } else {
              record.set('apiTags', {
                PAGE: value.sort(),
                BACKEND: []
              });
            }
          }
        } else if (_isArray(value) && !_isArray(oldValue)) {
          record.set('apiTags', {
            PAGE: value.sort(),
            BACKEND: []
          });
        } // }

      }
    },
    transport: {
      read: function read(config) {
        var params = config.params;
        var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions");
        var data = {};

        if (formRecord.toData()[0]) {
          var _formRecord$toData$ = formRecord.toData()[0],
              _formRecord$toData$$c = _formRecord$toData$.code,
              code = _formRecord$toData$$c === void 0 ? '' : _formRecord$toData$$c,
              _formRecord$toData$$p = _formRecord$toData$.path,
              path = _formRecord$toData$$p === void 0 ? '' : _formRecord$toData$$p,
              _formRecord$toData$$s = _formRecord$toData$.serviceName,
              serviceName = _formRecord$toData$$s === void 0 ? '' : _formRecord$toData$$s,
              _formRecord$toData$$f = _formRecord$toData$.fdLevel,
              fdLevel = _formRecord$toData$$f === void 0 ? '' : _formRecord$toData$$f,
              _formRecord$toData$$m = _formRecord$toData$.method,
              method = _formRecord$toData$$m === void 0 ? '' : _formRecord$toData$$m,
              _formRecord$toData$$l = _formRecord$toData$.labels,
              labels = _formRecord$toData$$l === void 0 ? '' : _formRecord$toData$$l,
              _formRecord$toData$$p2 = _formRecord$toData$.publicAccess,
              publicAccess = _formRecord$toData$$p2 === void 0 ? undefined : _formRecord$toData$$p2,
              _formRecord$toData$$l2 = _formRecord$toData$.loginAccess,
              loginAccess = _formRecord$toData$$l2 === void 0 ? undefined : _formRecord$toData$$l2,
              _formRecord$toData$$w = _formRecord$toData$.within,
              within = _formRecord$toData$$w === void 0 ? undefined : _formRecord$toData$$w,
              _formRecord$toData$$s2 = _formRecord$toData$.signAccess,
              signAccess = _formRecord$toData$$s2 === void 0 ? undefined : _formRecord$toData$$s2;
          var obj = {
            publicAccess: _isUndefined(publicAccess) || publicAccess === null ? undefined : !!publicAccess,
            loginAccess: _isUndefined(loginAccess) || loginAccess === null ? undefined : !!loginAccess,
            within: _isUndefined(within) || within === null ? undefined : !!within,
            signAccess: _isUndefined(signAccess) || signAccess === null ? undefined : !!signAccess
          };
          data = filterNullValueObject(_objectSpread({
            code: code,
            path: path,
            serviceName: serviceName,
            fdLevel: fdLevel,
            method: method,
            labels: labels
          }, obj));
        }

        return {
          data: data,
          params: params,
          url: url,
          method: 'GET'
        };
      },
      update: function update(config) {
        var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions/update") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions/update");
        var _config$data = config.data,
            data = _config$data === void 0 ? [] : _config$data,
            dataSet = config.dataSet;

        var _data$ = data[0],
            _status = _data$._status,
            apiTags = _data$.apiTags,
            pageTag = _data$.pageTag,
            other = _objectWithoutProperties(_data$, _excluded);

        var tags = (dataSet.labelList || []).filter(function (item) {
          return (data[0].apiTags.PAGE || []).includes(item.name);
        }).map(function (item) {
          return {
            id: item.id,
            _token: item._token
          };
        });
        return {
          data: _objectSpread(_objectSpread({}, other), {}, {
            labels: tags
          }),
          url: url,
          method: 'PUT'
        };
      },
      destroy: function destroy(config) {
        var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions");
        var _config$params = config.params,
            params = _config$params === void 0 ? {} : _config$params,
            _config$data2 = config.data,
            data = _config$data2 === void 0 ? [] : _config$data2;
        return {
          data: data,
          params: params,
          url: url,
          method: 'DELETE'
        };
      }
    }
  };
}

var tenantDS = {
  autoQuery: true,
  dataKey: 'content',
  cacheSelection: true,
  queryFields: [{
    name: 'tenantNum',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tenantNum').d('租户编码'),
    unique: true
  }, {
    name: 'tenantName',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tenantName').d('租户名称'),
    unique: true
  }],
  fields: [{
    name: 'tenantId',
    type: 'number',
    label: intl.get('ciam.apiManagement.model.apiManagement.tenantId').d('租户id'),
    unique: true
  }, {
    name: 'tenantNum',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tenantNum').d('租户编码'),
    unique: true
  }, {
    name: 'tenantName',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tenantName').d('租户名称'),
    unique: true
  }],
  events: {
    query: function query(_ref3) {
      var dataSet = _ref3.dataSet;
      dataSet.unSelectAll();
      dataSet.clearCachedSelected();
    }
  },
  transport: {
    read: function read(config) {
      var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/tenants") : "".concat(CPAAS_IAM, "/v1/tenants");
      return _objectSpread(_objectSpread({}, config), {}, {
        url: url,
        method: 'GET'
      });
    }
  }
}; // 租户API管理查询列

var tenantFormDS = {
  fields: [{
    name: 'tenantIdLov',
    type: 'object',
    lovCode: 'CPFM.TENANT',
    label: intl.get('ciam.apiManagement.model.apiManagement.tenant').d('租户'),
    noCache: true,
    ignore: 'always'
  }, {
    name: 'tenantName',
    type: 'string',
    bind: 'tenantIdLov.tenantName'
  }, {
    name: 'tenantId',
    type: 'string',
    bind: 'tenantIdLov.tenantId'
  }, {
    name: 'code',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.code').d('权限编码')
  }, {
    name: 'path',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.path').d('路径')
  }, {
    name: 'serviceNameLov',
    type: 'object',
    lovCode: 'CADM.SERVICE_CODE',
    label: intl.get('ciam.apiManagement.model.apiManagement.serviceName').d('服务名称'),
    ignore: 'always',
    noCache: true
  }, {
    name: 'serviceName',
    type: 'string',
    bind: 'serviceNameLov.serviceName'
  }, {
    name: 'method',
    type: 'string',
    lookupCode: 'CIAM.REQUEST_METHOD',
    lovPara: {
      tag: 'show'
    },
    label: intl.get('ciam.apiManagement.model.apiManagement.method').d('请求方式')
  }, {
    name: 'publicAccess',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.publicAccess').d('是否公开接口')
  }, {
    name: 'loginAccess',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.loginAccess').d('是否登录可访问')
  }, {
    name: 'within',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.within').d('是否内部接口')
  }, {
    name: 'signAccess',
    type: 'number',
    lookupCode: 'CPFM.FLAG',
    defaultValue: undefined,
    label: intl.get('ciam.apiManagement.model.apiManagement.signAccess').d('是否签名接口')
  }, {
    name: 'labels',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tag').d('标签'),
    valueField: 'name',
    textField: 'name',
    transformRequest: function transformRequest(value) {
      return value.join(',');
    },
    multiple: true,
    options: optionDs
  }]
}; // 租户API管理表格DS

function tenantTableDS(record) {
  return {
    dataKey: 'content',
    cacheSelection: true,
    fields: [{
      name: 'id',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.id').d('权限id'),
      unique: true
    }, {
      name: 'code',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.code').d('权限编码')
    }, {
      name: 'path',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.path').d('路径')
    }, {
      name: 'method',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.method').d('请求方式')
    }, {
      name: 'methodMeaning',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.methodMeaning').d('请求方式')
    }, {
      name: 'fdLevel',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.fieldLevel').d('权限层级')
    }, {
      name: 'levelMeaning',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.levelMeaning').d('权限层级')
    }, {
      name: 'description',
      type: 'intl',
      label: intl.get('ciam.apiManagement.model.apiManagement.description').d('描述')
    }, {
      name: 'action',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.action').d('方法名')
    }, {
      name: 'resource',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.resource').d('资源类型')
    }, {
      name: 'serviceName',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.serviceName').d('服务名称')
    }, {
      name: 'publicAccess',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.publicAccess').d('是否公开接口')
    }, {
      name: 'loginAccess',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.loginAccess').d('是否登录可访问')
    }, {
      name: 'within',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.within').d('是否内部接口')
    }, {
      name: 'signAccess',
      type: 'boolean',
      label: intl.get('ciam.apiManagement.model.apiManagement.signAccess').d('是否签名接口')
    }, {
      name: 'objectVersionNumber',
      type: 'number',
      label: intl.get('ciam.apiManagement.model.apiManagement.objectVersionNumber').d('版本')
    }, {
      name: 'tenantName',
      type: 'string',
      label: intl.get('ciam.apiManagement.model.apiManagement.tenantName').d('租户名称')
    }],
    events: {
      query: function query(_ref4) {
        var dataSet = _ref4.dataSet;
        dataSet.unSelectAll();
        dataSet.clearCachedSelected();
      }
    },
    transport: {
      read: function read(config) {
        var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/permissions/tenant") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions/tenant");
        var params = config.params;
        var data = {};

        if (record.toData()[0]) {
          var _record$toData$ = record.toData()[0],
              _record$toData$$tenan = _record$toData$.tenantId,
              tenantId = _record$toData$$tenan === void 0 ? '' : _record$toData$$tenan,
              _record$toData$$code = _record$toData$.code,
              code = _record$toData$$code === void 0 ? '' : _record$toData$$code,
              _record$toData$$path = _record$toData$.path,
              path = _record$toData$$path === void 0 ? '' : _record$toData$$path,
              _record$toData$$servi = _record$toData$.serviceName,
              serviceName = _record$toData$$servi === void 0 ? '' : _record$toData$$servi,
              _record$toData$$metho = _record$toData$.method,
              method = _record$toData$$metho === void 0 ? '' : _record$toData$$metho,
              _record$toData$$publi = _record$toData$.publicAccess,
              publicAccess = _record$toData$$publi === void 0 ? undefined : _record$toData$$publi,
              _record$toData$$login = _record$toData$.loginAccess,
              loginAccess = _record$toData$$login === void 0 ? undefined : _record$toData$$login,
              _record$toData$$withi = _record$toData$.within,
              within = _record$toData$$withi === void 0 ? undefined : _record$toData$$withi,
              _record$toData$$signA = _record$toData$.signAccess,
              signAccess = _record$toData$$signA === void 0 ? undefined : _record$toData$$signA,
              _record$toData$$label = _record$toData$.labels,
              labels = _record$toData$$label === void 0 ? '' : _record$toData$$label;
          var obj = {
            publicAccess: _isUndefined(publicAccess) || publicAccess === null ? undefined : !!publicAccess,
            loginAccess: _isUndefined(loginAccess) || loginAccess === null ? undefined : !!loginAccess,
            within: _isUndefined(within) || within === null ? undefined : !!within,
            signAccess: _isUndefined(signAccess) || signAccess === null ? undefined : !!signAccess
          };
          data = filterNullValueObject(_objectSpread({
            tenantId: tenantId,
            code: code,
            path: path,
            serviceName: serviceName,
            method: method,
            labels: labels
          }, obj));
        }

        return {
          data: data,
          params: params,
          url: url,
          method: 'GET'
        };
      },
      destroy: function destroy(config) {
        var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/permissions/revoke") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions/revoke");
        var _config$params2 = config.params,
            params = _config$params2 === void 0 ? {} : _config$params2,
            _config$data3 = config.data,
            data = _config$data3 === void 0 ? [] : _config$data3;
        return {
          data: data,
          params: params,
          url: url,
          method: 'DELETE'
        };
      }
    }
  };
} // 刷新api表单DS


var refreshDS = {
  autoQueryAfterSubmit: false,
  fields: [{
    name: 'serviceName',
    label: intl.get('ciam.apiManagement.model.apiManagement.refreshServiceName').d('服务名'),
    required: true,
    type: 'string'
  }, {
    name: 'metaVersion',
    label: intl.get('ciam.apiManagement.model.apiManagement.metaVersion').d('服务标记版本'),
    required: true,
    type: 'string'
  }, {
    name: 'cleanPermission',
    label: intl.get('ciam.apiManagement.model.apiManagement.cleanPermission').d('是否清除过期权限'),
    type: 'boolean',
    defaultValue: false
  }],
  transport: {
    create: function create(_ref5) {
      var data = _ref5.data;

      var _ref6 = Array.isArray(data) ? data[0] : {},
          __id = _ref6.__id,
          _status = _ref6._status,
          other = _objectWithoutProperties(_ref6, _excluded2);

      var url = "".concat(CPAAS_IAM, "/v1/tool/permission/fresh");
      return {
        url: url,
        method: 'POST',
        params: other,
        data: {}
      };
    }
  }
};
var drawerDS = {
  autoQuery: false,
  autoQueryAfterSubmit: false,
  fields: [{
    name: 'description',
    type: 'intl',
    label: intl.get('ciam.apiManagement.model.apiManagement.description').d('描述'),
    maxLength: 1024
  }, {
    name: 'tag',
    type: 'string',
    label: intl.get('ciam.apiManagement.model.apiManagement.tag').d('标签')
  }],
  transport: {
    read: function read() {
      var url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/labels/by-type") : "".concat(CPAAS_IAM, "/v1/labels/by-type");
      return {
        url: url,
        method: 'GET',
        params: {
          type: 'API'
        }
      };
    }
  }
};
export { formDS, tableDS, tenantDS, refreshDS, tenantFormDS, tenantTableDS, drawerDS };